package yxy.game.pm2.bean.db.newDao;

import yxy.apple.db.DbManager;
import yxy.game.pm2.bean.db.po.HeroPo;

import java.util.ArrayList;
import java.util.List;

public class HeroDAO {

    private static final String
            REPLACE_ONE = "replace into `hero` (`pCode`, `code`, `dataId`, `level`, `star`, `stage`, `equips`, `soulA`, `soulB`, `magicA`, `magicB`, `magicC`, `lock`, `starUpCost`,`updateTime`) values" +
            " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
            SELECT_ALL = "select * from `hero` where `pCode` = ?",
            DELETE = "delete from `hero` where `pCode` = ? and `updateTime` != ?";

    public static boolean replace(List<HeroPo> heroPos, long updateTime) {
        List<Object[]> list = new ArrayList<>();
        for (HeroPo hero : heroPos) {
            Object[] objects = new Object[]{hero.getpCode(), hero.getCode(), hero.getDataId(), hero.getLevel(), hero.getStar(),hero.getStage(), hero.getEquips(),
                    hero.getSoulA(), hero.getSoulB(), hero.getMagicA(), hero.getMagicB(), hero.getMagicC(), hero.isLock(), hero.getStarUpCost(), updateTime};
            list.add(objects);
        }
        int[] ints = DbManager.instance().executeBatchCommand(REPLACE_ONE, list);
        return ints.length == heroPos.size();
    }

    public static List<HeroPo> getList(String pCode) {
        return DbManager.instance().executeQueryObjectList(SELECT_ALL, new Object[]{pCode}, HeroPo.builder);
    }

    public static void delete(String pCode, long updateTime) {
        DbManager.instance().executeCommand(DELETE, new Object[]{pCode, updateTime});
    }
}
